package com.wang.auth.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.auth.mapper.ProcessTemplateMapper;
import com.wang.auth.service.ProcessService;
import com.wang.auth.service.ProcessTemplateService;
import com.wang.auth.service.ProcessTypeService;
import com.wang.model.process.ProcessTemplate;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.model.process.ProcessType;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 审批模板 服务实现类
 * </p>
 *
 * @author wang
 * @since 2023-04-17
 */
@Service
public class ProcessTemplateServiceImpl extends ServiceImpl<ProcessTemplateMapper, ProcessTemplate> implements ProcessTemplateService {

    @Resource
    ProcessTypeService processTypeService;

    @Resource
    ProcessService processService;

    @Override
    public Page<ProcessTemplate> selectPageProcessTemplate(Long page, Long limit) {
        Page<ProcessTemplate> processTemplatePage = this.page(new Page<>(page,limit));
        List<ProcessTemplate> processTemplateList = processTemplatePage.getRecords();
        processTemplateList.stream().map(processTemplate -> {
            ProcessType processType = processTypeService.getById(processTemplate.getProcessTypeId());
            String typeName = processType.getName();
            processTemplate.setProcessTypeName(typeName);
            return processTemplate;
        }).collect(Collectors.toList());
        return processTemplatePage.setRecords(processTemplateList);

    }

    @Override
    public void publish(Long id) {
        //修改模板状态，1为发布，0为未发布
        ProcessTemplate processTemplate = this.getById(id);
        processTemplate.setStatus(1);
        this.updateById(processTemplate);
        String path = processTemplate.getProcessDefinitionPath();
        if (StringUtils.isNotBlank(path)){
            processService.deployByZip(path);
        }
    }
}
